<template>
    <div class="container">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
            <el-form-item label="请求是否通过" label-width="120px">
                <el-radio-group v-model="form.resource" prop="resource" @change="choseState(form.resource)">
                    <el-radio label="0">待处理</el-radio>
                    <el-radio label="1">通过</el-radio>
                    <el-radio label="2">不通过</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="不通过原因" prop="desc" label-width="120px">
                <el-input type="textarea" v-model="form.desc"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('form')">确认</el-button>
                <el-button @click="resetForm('form')">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "",
        data () {
            var checkDesc = (rule, value, callback) => {
                if(this.form.resource=='2'){
                    if (value === '') {
                        callback(new Error('请填写请求不通过原因'));
                    }else {
                        callback();
                    }
                }else {
                    callback();
                }
            };
            return {
                form:{
                    resource:'0',
                    desc:''
                },
                rules: {
                    resource:[
                        { required: true, message: '请选择请求是否通过', trigger: 'change' }
                    ],
                    desc:[
                        { validator: checkDesc, trigger: 'blur' }
                    ]
                }
            }
        },
        props: ['uid','state'],
        methods:{
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 获取订单信息
            getInfo(){
                if(!this.uid) {
                    return;
                }
            },
            // 处理订单是否通过
            choseState(value){
                console.log(value)
            },
            // 提交处理信息
            submitForm(formName){
                console.log(formName)
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let obj = this.state==8?{
                            'orderId': this.uid,
                            'state': this.form.resource,
                            'disagreenReason': this.form.desc,
                        }:{
                            'orderId': this.uid,
                            'isPass': this.form.resource,
                            'disagreenReason': this.form.desc,
                        };
                        console.log(this.state)
                        let url = this.state==8?'/supply/order/handleOrderBackApply':'/supply/order/handleOrderCancelApply';
                        this.loadopen();
                        this.config.post(url, obj).then(res => {
                            this.loadclose();
                            console.log(res.data)
                            if (res.data.code === '0') {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            // 取消处理
            resetForm(){
                this.$emit('done', '');
            }
        }
    }
</script>

<style scoped>
label.el-form-item__label{
    width:120px !important;
}
</style>
